package com.ruoyi.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.domain.entity.CategoryModel;
import com.ruoyi.admin.domain.vo.response.category.CategoryPageResponse;
import com.ruoyi.admin.domain.vo.response.category.CategoryResponse;
import com.ruoyi.admin.mapper.CategoryMapper;
import com.ruoyi.admin.service.CategoryService;
import com.ruoyi.common.api.Result;
import com.ruoyi.common.api.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (Category)Service实现类
 *
 * @author jianhan
 * @date 2022-12-07 15:41:36
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryModel> implements CategoryService {

    private static final Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Override
    public Result<CategoryResponse> getId(Long categoryId) {
        CategoryModel categoryModel = this.getById(categoryId);
        if (categoryModel != null) {
            CategoryResponse categoryResponse = new CategoryResponse(categoryModel);
            return Result.success(categoryResponse);
        }
        return Result.failed(ResultCode.DATA_NOT_EXIST);
    }

    @Override
    public Result<CategoryPageResponse> page(Page<CategoryModel> page, CategoryModel searchModel) {
        QueryWrapper<CategoryModel> queryWrapper = queryWrapper(searchModel);

        Page<CategoryModel> pages = this.page(page, queryWrapper);
        CategoryPageResponse categoryPageResponse = new CategoryPageResponse(pages);
        page.getRecords().forEach((categoryModel) -> categoryPageResponse.getRecords().add(new CategoryResponse(categoryModel)));
        return Result.success(categoryPageResponse);

    }

    @Override
    public Result add(CategoryModel categoryModel) {
        List<CategoryModel> category = this.list(new QueryWrapper<CategoryModel>().eq("category", categoryModel.getCategory()));
        if (category.size() > 0 && category != null) {
            return Result.failed("已经存在该分类");
        }
        Boolean result = this.save(categoryModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("添加失败");
    }

    @Override
    public Result update(CategoryModel categoryModel) {
        Boolean result = this.updateById(categoryModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("更新失败");
    }

    @Override
    public Result delete(Long categoryId) {
        Boolean result = this.removeById(categoryId);
        return Result.judge(result);

    }

    private QueryWrapper<CategoryModel> queryWrapper(CategoryModel categoryModel) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        
        /*
           写搜索条件
        */
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }

}
